$(function () {
    var queryUrl = '../toolscheckplanitemtools/list';
    var itemId = getQueryString('itemId');
    if (itemId) {
        queryUrl += '?itemId=' + itemId;
        vm.itemId = itemId;
    }
    // 获取产线deptId
    var productLine = getQueryString('productLine');
    if (productLine) {
        vm.productLine = productLine;
    }
    vm.status = getQueryString('status');
    var colModelArray = [
        {label: '名称', name: 'name', index: 'NAME'},
        {label: 'ERP编码', name: 'erpCode', index: 'ERP_CODE', width: 120},
        {label: '图号', name: 'mapNo', index: 'MAP_NO'},
        {label: '自制图号', name: 'createNo', index: 'CREATE_NO'},
        {label: '型号规格', name: 'type', index: 'TYPE'},
        {label: '工装类别', name: 'toolsTypeName', index: 'TOOLS_TYPE', width: 60},
        {label: '现场装机总量', name: 'capacity', index: 'CAPACITY', width: 60},
        {
            label: '校验标准', width: 60, align: 'center', sortable: false,
            formatter: function (value, col, row) {
                return "<button title='查看周期性校验标准' class='btn btn-outline btn-info' onclick='vm.viewCheckStandard(\"" + row.toolsFileId + "\", \"" + row.controlWay + "\")'><i class='fa fa-file-text-o'></i></button>";
            }
        },
        {label: '工装档案ID', name: 'toolsFileId', hidden: true},
        {label: '检测计划明细ID', name: 'itemId', hidden: true},
        {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
    ];
    // 只有处于新建或者审批遭拒的检测计划才可以进行编辑
    if(vm.status && (vm.status == '0' || vm.status == '4')){
        colModelArray[6].editable = true;
        colModelArray[6].editrules = {number:true};
    }
    $("#jqGrid").Grid({
        url: queryUrl,
        colModel: colModelArray,
        forceFit : true,
        cellEdit: true,
        cellsubmit: 'remote',
        cellurl : '../toolscheckplanitemtools/update'
    });
});

let vm = new Vue({
    el: '#rrapp',
    data: {
        itemId: '', // 工装周期性校验计划ID
        productLine: '', // 产线ID
        status:'' // 工装检测进度
    },
    methods: {
        viewCheckStandard: function (toolsFileId,controlWay) {
            // 查看周期性检验标准
            if(!controlWay || controlWay.indexOf('02') < 0){
                iview.Message.error("只可以查看周期性精度检测的记录！");
                return;
            }
            openWindow({
                type: 2,
                title: '<i class="fa fa-bars"></i> 周期性精密校验判定标准',
                content: '../tools/toolscheckjudgestandard.html?toolsFileId=' + toolsFileId
            })
        },
        query: function () {
            vm.reload();
        },
        add: function () {
            openWindow({
                type: 2,
                title: '<i class="fa fa-file-text-o"></i> 选择工装',
                content: '../tools/toolslistSelect.html?controlWay=02&itemId=' + vm.itemId + '&productLine=' + vm.productLine,
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var iframeWin = window["layui-layer-iframe" + index];
                    var res = iframeWin.getToolsList();
                    if (res.length == 0) {
                        iview.Message.error("请选择一条记录");
                    } else {
                        var param = {itemId: vm.itemId, toolsFileIdList: res};
                        Ajax.request({
                            url: "../toolscheckplanitemtools/save",
                            params: JSON.stringify(param),
                            type: "POST",
                            contentType: "application/json",
                            successCallback: function (r) {
                                alert('操作成功', function (index) {
                                    vm.reload();
                                });
                            }
                        });
                    }
                    layer.close(index);
                }
            })
        },
        update: function (event) {
            let id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            let url = vm.toolsCheckPlanItemTools.id == (null || '') ? "../toolscheckplanitemtools/save" : "../toolscheckplanitemtools/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.toolsCheckPlanItemTools),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            let ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../toolscheckplanitemtools/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../toolscheckplanitemtools/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.toolsCheckPlanItemTools = r.toolsCheckPlanItemTools;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {},
                page: page
            }).trigger("reloadGrid");
        },
        reloadSearch: function () {
            // vm.q = {
            //     name: ''
            // };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
    }
});
